package com.hospitalsystem.service.impl;

import com.hospitalsystem.dao.CheckListDao;
import com.hospitalsystem.entity.CheckList;
import com.hospitalsystem.service.CheckListService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CheckList)表服务实现类
 *
 * @author makejava
 * @since 2024-12-28 19:31:42
 */
@Service("checkListService")
public class CheckListServiceImpl implements CheckListService {
    @Resource
    private CheckListDao checkListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param  checkId
     * @return 实例对象
     */
    @Override
    public CheckList queryById(String checkId) {
        return this.checkListDao.queryById(checkId);
    }

    /**
     * 分页查询
     *
     *
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<CheckList> queryByPage(PageRequest pageRequest) {
        int page = (int) pageRequest.getPageNumber(); // 页码，从 1 开始
        int pageSize = pageRequest.getPageSize();

        // 计算 offset，页码从1开始，所以 offset = (page - 1) * pageSize
        int offset = (page - 1) * pageSize;

        long total = this.checkListDao.count();
        List<CheckList> doctors = this.checkListDao.queryAll(offset, pageSize); // 传递其他必要的过滤条件
        return new PageImpl<>(doctors, pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param checkList 实例对象
     * @return 实例对象
     */
    @Override
    public CheckList insert(CheckList checkList) {
        this.checkListDao.insert(checkList);
        return checkList;
    }

    /**
     * 修改数据
     *
     * @param checkList 实例对象
     * @return 实例对象
     */
    @Override
    public CheckList update(CheckList checkList) {
        this.checkListDao.update(checkList);
        return this.queryById(checkList.getCheckId());
    }

    /**
     * 通过主键删除数据
     *
     * @param  checkId
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String checkId) {
        return this.checkListDao.deleteById(checkId) > 0;
    }

    @Override
    public List<CheckList> findAll(){
        return this.checkListDao.findAll();
    };
}
